package com.obas.integration.element.telnet.loader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.ease.common.util.OutputUtil;
import com.ease.common.vo.editor.AbstractObjectEditor;
import com.ease.integration.telent.TelnetClient;
import com.ease.integration.telent.TelnetExecutionCallback;
import com.obas.integration.element.telnet.editor.HWME60TelnetEditor;
import com.obas.model.business.DeviceMacAddress;

public class HWME60TelnetDeviceMacAddressLoader extends HWDefaultTelnetDeviceMacAddressLoader {

	public AbstractObjectEditor<DeviceMacAddress> getRawEditor() {
		return new HWME60TelnetEditor();
	}

	public List<DeviceMacAddress> editTextAsObject(String rawDeviceMacAddress) {
		AbstractObjectEditor<DeviceMacAddress> rawEditor = this.getRawEditor();

		rawDeviceMacAddress = StringUtils.replace(rawDeviceMacAddress, "[42D", "\r\n");
		String[] rows = StringUtils.delimitedListToStringArray(rawDeviceMacAddress, "\r\n");
		if (notEmpty(rows)) {
			List<DeviceMacAddress> deviceMacAddresses = new ArrayList<DeviceMacAddress>();
			boolean start = false;
			for (String row : rows) {

				if (!notEmpty(row)) {
					continue;
				}
				if (row.startsWith("  -----------------------------------")) {
					continue;
				}
				if (row.startsWith("          IPv6 address")) {
					start = true;
					continue;
				}
				if (row.startsWith("  ---- More ----")) {
					continue;
				}
				if (row.startsWith("  Total users")) {
					start = false;
					continue;
				}

				if (row.length() <= 70) {
					continue;
				}
				if (true == start) {
					rawEditor.setAsText(row);
					DeviceMacAddress deviceMacAddress = rawEditor.getObject();
					if (null != deviceMacAddress) {
						deviceMacAddresses.add(deviceMacAddress);
					}
				}
			}

			appendVlan(deviceMacAddresses);

			return escapeEmpty(deviceMacAddresses);
		}
		return null;
	}

	public void appendVlan(final List<DeviceMacAddress> deviceMacAddresses) {

		if (null != deviceMacAddresses) {

			this.execute(new TelnetExecutionCallback<String>() {

				public String execute(TelnetClient telnetClient) {
					//
					for (DeviceMacAddress deviceMacAddress : deviceMacAddresses) {
						telnetClient.request("display access-user user-id " + deviceMacAddress.getUserId());
						String userInfo = telnetClient.response(null);
						String[] vlans = getUserVlan(userInfo);
						if (null != vlans) {
							deviceMacAddress.setVlan(vlans[0]);
							deviceMacAddress.setPeVlan(vlans[0]);
							deviceMacAddress.setCeVlan(vlans[1]);
						}
					}

					return null;
				}

			});

		}
	}

	public String[] getUserVlan(String userInfo) {
		String[] rows = StringUtils.delimitedListToStringArray(userInfo, "\r\n");
		if (notEmpty(rows)) {
			String prefix = "  User access PeVlan/CeVlan     : ";
			for (String row : rows) {

				if (!notEmpty(row)) {
					continue;
				}

				if (row.startsWith(prefix)) {
					row = row.substring(prefix.length() + 1, row.length()).trim();
					String[] vlans = StringUtils.delimitedListToStringArray(row, "/");
					if (null != vlans && 2 == vlans.length) {
						return vlans;
					}
					break;
				}
			}
		}
		return null;
	}

	String domain = "km56adsl";

	public String doLoadRawDeviceMacAddress() {

		return this.execute(new TelnetExecutionCallback<String>() {
			public String execute(TelnetClient telnetClient) {
				// telnetClient.request("display access-user");
				telnetClient.request("display access-user domain " + domain);

				// String[] userIds= getUserIds();

				return telnetClient.response(null);
			}
		});
	}

	public static void main(String[] args) {

		HWME60TelnetDeviceMacAddressLoader retriever = new HWME60TelnetDeviceMacAddressLoader();

		retriever.setHost("222.172.255.198");
		retriever.setPort(23);
		retriever.setUsername("huawei");
		retriever.setPassword("huawei");

		retriever.setUsernamePrompt("Username:");
		retriever.setPasswordPrompt("Password:");
		retriever.setMorePrompt("  ---- More ----");
		// retriever.setCommand("disp mac-address");

		// System.out.println();

		OutputUtil.p(retriever.loadDeviceMacAddress());

	}

	// public static void main(String[] args) {
	// String rawDeviceMacAddress = FileUtil.readClassPathResourceToString(HWDefaultTelnetDeviceMacAddressLoader.class,
	// "HW S9303.txt");
	// List<DeviceMacAddress> deviceMacAddresses = new
	// HWME60TelnetDeviceMacAddressLoader().editTextAsObject(rawDeviceMacAddress);
	// OutputUtil.p(deviceMacAddresses);
	// }
}
